package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Role;
import com.pro_one.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 动态菜单功能实现
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 添加角色
     * @param role 角色对象
     * @param permissionIds 与角色关联的权限id们
     * @param menuIds 与角色关联的菜单id们
     * @return
     */
    @RequestMapping("/addRole")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result addRole(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.addRole(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }


    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/deleteRole")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteRole(Integer id) {
        try {
            roleService.deleteRole(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }


    }

    /**
     * 修改角色
     * @param role 角色对象
     * @param permissionIds 与角色关联的权限id们
     * @param menuIds 与角色关联的菜单id们
     * @return
     */
    @RequestMapping("/updateRole")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result updateRole(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.updateRole(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }


    }

    /**
     * 查询所有角色
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllRole")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findAllRole(@RequestBody QueryPageBean queryPageBean) {
            PageResult roles = roleService.findAllRole(queryPageBean);
            return roles;
    }

    /**
     * 根据用户id查询所有角色，主要用于回显数据
     * @param queryPageBean
     * @param userId
     * @return
     */
    @RequestMapping("/findAllRoleByUserId")
    public PageResult findAllRoleByUserId(@RequestBody QueryPageBean queryPageBean,Integer userId){
        PageResult allRoleByUserId = roleService.findAllRoleByUserId(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), userId);
        return allRoleByUserId;
    }

}
